/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlets;

import Beans.beanPersonal;
import Conexion.cConexion;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Emanuel
 */
@WebServlet(name = "srvPersonal", urlPatterns = {"/srvPersonal"})
public class srvPersonal extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            //obtiene el tipo de boton
            String accion = request.getParameter("accion");

            //accion si el boton es de insertar
            if (accion.equals("INSERTAR")) {
                try {
                    //realiza la coneccion
                    Connection cnn = cConexion.conectar_ds();
                    PreparedStatement psta;
                    //llama a la accion de el bean
                    psta = cnn.prepareStatement(beanPersonal.insertar);
                    //obtiene los datos desde la jsp
                    String carnet_p = request.getParameter("carnet_p");
                    String nombre_pe = request.getParameter("nombre_pe");
                    String apellido_pe = request.getParameter("apellido_pe");
                    String telefono_pe = request.getParameter("telefono_pe");
                    String correo_pe = request.getParameter("correo_pe");
                    String usuario = request.getParameter("usuario");
                    String clave = request.getParameter("clave");
                    int codigo_per = Integer.parseInt(request.getParameter("codigo_per"));
                    //captura el dato y lo perara para enviar
                    psta.setString(1, carnet_p);
                    psta.setString(2, nombre_pe);
                    psta.setString(3, apellido_pe);
                    psta.setString(4, telefono_pe);
                    psta.setString(5, correo_pe);
                    psta.setString(6, usuario);
                    psta.setString(7, clave);
                    psta.setInt(8, codigo_per);

                    //ejecuta la accion del bean
                    psta.executeUpdate();

                    String insertok = this.getServletContext().getContextPath() + "/admin/MESSAGE/insertok.jsp";
                    response.sendRedirect(insertok);
                } catch (SQLException e) {

                    String error = this.getServletContext().getContextPath() + "/admin/MESSAGE/error.jsp";
                    response.sendRedirect(error);
                }
                //accion si el boton es modificar
            } else if (accion.equals("MODIFICAR")) {
                try {
                    Connection cnn = cConexion.conectar_ds();
                    PreparedStatement psta;
                    psta = cnn.prepareStatement(beanPersonal.modificar);


                    String carnet_p = request.getParameter("carnet_p");
                    String nombre_pe = request.getParameter("nombre_pe");
                    String apellido_pe = request.getParameter("apellido_pe");
                    String telefono_pe = request.getParameter("telefono_pe");
                    String correo_pe = request.getParameter("correo_pe");
                    String usuario = request.getParameter("usuario");
                    String clave = request.getParameter("clave");
                    int codigo_per = Integer.parseInt(request.getParameter("codigo_per"));


                    psta.setString(8, carnet_p);
                    psta.setString(1, nombre_pe);
                    psta.setString(2, apellido_pe);
                    psta.setString(3, telefono_pe);
                    psta.setString(4, correo_pe);
                    psta.setString(5, usuario);
                    psta.setString(6, clave);
                    psta.setInt(7, codigo_per);

                    psta.executeUpdate();

                    String updateok = this.getServletContext().getContextPath() + "/admin/MESSAGE/updateok.jsp";
                    response.sendRedirect(updateok);
                } catch (SQLException e) {

                    String error = this.getServletContext().getContextPath() + "/admin/MESSAGE/error.jsp";
                    response.sendRedirect(error);
                }
                //accion si el boton es eliminar
            } else {
                try {
                    Connection cnn = cConexion.conectar_ds();
                    PreparedStatement psta;
                    psta = cnn.prepareStatement(beanPersonal.eliminar);

                    String carnet_p = request.getParameter("carnet_p");

                    psta.setString(1, carnet_p);

                    psta.executeUpdate();

                    String deleteok = this.getServletContext().getContextPath() + "/admin/MESSAGE/deleteok.jsp";
                    response.sendRedirect(deleteok);
                } catch (SQLException e) {

                    String error = this.getServletContext().getContextPath() + "/admin/MESSAGE/error.jsp";
                    response.sendRedirect(error);
                }
            }

            String redirect = this.getServletContext().getContextPath() + "/admin/admin-personal.jsp";

            HttpSession session = request.getSession();

            session.setAttribute("url", redirect);

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
